'use strict';

const Controller = require('egg').Controller;

class JwtController extends Controller {

    async index() {
        // this.ctx.body = "hellp jwt";
        const { ctx, app } = this;
        console.log(app, `@app`);
        console.log(ctx, `@ctx`);
        /* 结构语法的不当使用(一种常见的错用形式就是,和成员访问运算符混合使用,导致被解构对象出现偏差) 
        例如,已知,app对象中存在有一个属性jwt,现在你需要将他复制给一个变量以便后续的多次引用.*/
        // const { jwt } = app.jwt;
        // const{jwt}=app;

        const { jwt: appJwt = `deconstruct app.jwt` } = app;
        const { jwt: confJwt = `deconstruct app.config.jwt` } = app.config;
        // console.log(app.jwt, `@app.jwt`);
        // console.log(appJwt, `@appConfig`);
        // console.log(confJwt, `@confJwt`);
        const user = {
            username: "cxxu"
        };
        const { secret } = confJwt;
        const token = appJwt.sign(user, secret);
        const decode = appJwt.verify(token, secret);
        // const{username}=decode;
        /* ctx.body  */
        console.log(typeof (ctx.body), `@test type of ctx.body`);
        console.log(decode, `@decode`);
        let { body: cbody = `ctx.body deconstruct` } = ctx;
        // 渲染token/decode
        // console.log(cbody, `@cbody`);
        // console.log(cbody, `@after token added`);
        // ctx.body = token;
        ctx.body = decode;

        cbody = `test cbody`;
        console.log(cbody, `@cbody`);

    }
}

module.exports = JwtController;
